package iosr11.ui.validators;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

public class EmailValidator implements Validator {
	
	private static final String errorDetail = "Blad: loginem jest e-mail w domenie gmail.com.";
	private static final String	error = "Bledny login.";
	
	@Override
	public void validate(FacesContext context, UIComponent component, Object value)
			throws ValidatorException {
		if (value == null) return;
		
		if(!emailCheck(value.toString())){
			FacesMessage msg = new FacesMessage();	//Messages.getMessage("iosr11.ui.common.messages", "badLogin", null);
			msg.setDetail(errorDetail);
			msg.setSummary(error);
			msg.setSeverity(FacesMessage.SEVERITY_ERROR);
			throw new ValidatorException(msg);
		}

	}

	private boolean emailCheck(String mail) {
		if(mail.contains("@gmail.com")) return true;
		
		return false;
	}

}
